<template>
  <div class="beds-add">
    <DynamicForm
      ref="dynamicFormRef"
      :rulesForm="state.productAdd"
      :rules="state.rules"
      :refresh="state.refresh"
      @click="callBack"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, defineEmits, reactive } from "vue";
import { ElMessage } from "element-plus";
import moment from "moment"; //时间
import DynamicForm from "@/components/layout/form/dynamic-form.vue"; //表单
import {
  productAddForms, //表单
  productAddRules, //验证规则
} from "@/assets/script/middle/forms/system/organ/add-form";
import { listCustomerPageApi } from "@/api/product"; //接口
import {
  insertRmsOrganizationApi, //新增
  findRmsOrganizationPageListApi, //查询数据
  modifyRmsOrganizationApi, //编辑
} from "@/api/system"; //接口
import { listConstByDictionaryCodesApi } from "@/api/dictionary"; //数据字典

import { useRoute } from "vue-router"; //路由
import { ConsoleSqlOutlined } from "@ant-design/icons-vue";
const emits = defineEmits<{ (e: "appClick", data: object): void }>();
const dynamicFormRef = ref(DynamicForm);

const router = useRoute();
const state = reactive({
  productAdd: JSON.parse(JSON.stringify(productAddForms)) as any, //表单
  rules: productAddRules as any, //验证规则
  refresh: 0 as number,
  bed: {} as any,
});
const card = state.productAdd[0].card;
const roleList = ref([]) as any; //角色数据
onMounted(async () => {
  //判断是否是编辑
  if (router.query && router.query.id) {
    findByIdFun(router.query.id as any);
  }
});
//查询数据
const findByIdFun = async (id: string) => {
  await findRmsOrganizationPageListApi({ id }).then((data: any) => {
    if (data.code == 0) {
      let datas = data.data[0];
      console.log(datas);
      state.bed = datas;
      card.forEach((e: any) => {
        if (!e.line) {
          switch (e.name) {
            case "names":
              e[e.name] = state.bed["name"];
              break;
            default:
              e[e.name] = state.bed[e.name];
              break;
          }
        }
      });
      state.refresh = Math.random();
    }
  });
};

const callBack = (data: any) => {
  switch (data.type || "") {
    //提交表单
    case "submit":
      let datas = data.data;
      console.log(datas);
      let params = datas;
      params.name = datas.names;
      params.birthday =
        moment(datas.birthday).format("YYYY-MM-DDTHH:mm:ss") + "Z";
      params.userTypeName = datas.userTypeIdName;
      insertFun(params);
      break;
  }
};
//新增 编辑
const insertFun = (params: any) => {
  //判断是否是编辑;
  if (router.query && router.query.id) {
    params.id = router.query.id;
    console.log(params);
    modifyRmsOrganizationApi(params).then((data: any) => {
      if (data.code == 0) {
        ElMessage.success("修改成功");
        emits("appClick", { type: "closeTab", data: router.path });
      }
    });
  } else {
    console.log(params);
    //新增

    insertRmsOrganizationApi(params).then((data: any) => {
      if (data.code == 0) {
        ElMessage.success("新增成功");
        state.refresh = Math.random();
        emits("appClick", { type: "closeTab", data: router.path });
      }
    });
  }
};
</script>
